﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tasks.Dao;
using Tasks.Common;
using Tasks.Common.Entitys;
using System.Collections;
using MyLib.Util;
namespace Tasks.Business
{
    /// <summary>
    /// 权限业务逻辑类
    /// </summary>
    public class PowerBLL : MyBaseBLL
    {
        PowerActionDao actionDao = new PowerActionDao();
        PowerModuleDao moduleDao = new PowerModuleDao();
        PowerRoleDao roleDao = new PowerRoleDao();
        PWUser2RoleDao pwuserDao = new PWUser2RoleDao();
        PowerRole2Module2ActionDao module2ActionDao = new PowerRole2Module2ActionDao();
        public PowerBLL(int ownerID)
            : base(ownerID)
        {

        }

        /// <summary>
        /// 获取指定用户可使用的系统
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public IList<App> GetAppsByUserID(int userID)
        {
            return roleDao.FindAppByUserID(userID);
        }

        /// <summary>
        /// 获取指定角色可使用的系统
        /// </summary>
        /// <param name="roleID"></param>
        /// <returns></returns>
        public IList<App> GetAppsByRoleID(int roleID)
        {
            return roleDao.FindAppByRoleID(roleID);
        }

        /// <summary>
        /// 添加角色的应用程序
        /// </summary>
        /// <param name="roleID"></param>
        /// <param name="appIDs"></param>
        public void ModifyRole2App(int roleID, int[] appIDs)
        {
            roleDao.InsertRole2App(roleID, appIDs);
        }

        /// <summary>
        /// 获取所有系统
        /// </summary>
        /// <returns></returns>
        public IList<App> GetAllApps()
        {
            return roleDao.FindAllApp();
        }

        /// <summary>Implements <see cref="IPowerActionDao.Find"/></summary>
        public IList<UserPower> GetUserPowersByUserID(Int32 userID)
        {
            return actionDao.FindUserPowersByUserID(userID);
        }

        /// <summary>
        /// 创建角色
        /// </summary>
        public ExecuteResult CreateRole(PowerRole role)
        {
            role.OwnerID = this.OwnerID;
            roleDao.Insert(role);
            return new ExecuteResult() { ResultNo = 1, Message = "" };
        }

        public ExecuteResult ModifyRole(PowerRole role)
        {
            role.OwnerID = this.OwnerID;
            roleDao.Update(role);
            return new ExecuteResult() { ResultNo = 1, Message = "" };
        }

        /// <summary>
        /// 分配角色到用户
        /// </summary>
        /// <param name="roles"></param>
        /// <param name="userID"></param>
        public void AssignSingleRoleToUser(int roleID, int userID)
        {
            roleDao.AssignRolesToUser(roleID, userID);
        }

        /// <summary>
        /// 分配角色到指定对象(員工)
        /// </summary>
        /// <param name="roleIDs">角色ID字符串，用逗号隔开</param>
        public ExecuteResult AssignRoleToUser(string roleIDs, int userID)
        {
            int[] aryRoleID = StringUtil.StringToArray(roleIDs);
            roleDao.AssignRolesToUser(aryRoleID, userID);
            return new ExecuteResult() { ResultNo = 1, Message = "" };
        }

        /// <summary>
        /// 分配权限到指定角色
        /// </summary>
        /// <param name="actionIDs">角色ID字符串，用逗号隔开</param>
        public ExecuteResult AssignModuleToRole(string actionIDs, int RoleID)
        {
            int[] aryActionID = StringUtil.StringToArray(actionIDs);
            module2ActionDao.AssignModuleToRoles(aryActionID, RoleID);
            return new ExecuteResult() { ResultNo = 1, Message = "" };
        }
        /// <summary>
        /// 获取角色管理权限
        /// </summary>
        /// <param name="RoleID">角色ID</param>
        /// <returns></returns>
        public IList<PowerRole2Module2Action> Get2Module2ActionByRoleID(int RoleID)
        {
            return module2ActionDao.FindByRoleID(RoleID);
        }

        /// <summary>
        /// 根据userID获取角色权限
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public IList<PowerRole> GetPowerRoleByUserID(int userID)
        {
            return pwuserDao.FindPowerRoleByUserID(userID);
        }

        /// <summary>
        /// 根据userID获取角色权限
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public IList<PowerUser2Role> GetRoleByUserID(int userID)
        {
            return pwuserDao.FindByUserID(userID);
        }
        /// <summary>
        /// 根据条件获取已授权的账号列表
        /// </summary>
        /// <param name="condition">查询条件</param>
        /// <param name="pageSize">每页显示记录数</param>
        /// <param name="PageIndex"></param>
        /// <returns></returns>
        public IList<PowerUser2Role> GetAccountRole(Hashtable condition, int pageSize, int PageIndex)
        {
            return pwuserDao.SearchAccountRole(condition, pageSize, PageIndex);
        }

        /// <summary>
        /// 分配权限到角色
        /// </summary>
        /// <param name="actions">动作对象</param>
        public ExecuteResult AssignActionToRole(IList<PowerAction> actions, int roleID)
        {
            throw new System.NotImplementedException();
        }

        /// <summary>
        /// 通过用户ID获取权限的模块，动作
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public IList<PowerAction> GetActionsByUserID(int userID)
        {
            return actionDao.FindRoleActionByUserID(userID);
        }

        /// <summary>
        /// 通过用户ID获取权限的模块，动作
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public PowerModule GetModuleByName(string name)
        {
            return moduleDao.FindModuleByName(name);
        }

        /// <summary>
        /// 获取指定用户拥有的模块
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public IList<PowerModule> GetModulesByUserID(int userID)
        {
            return moduleDao.FindModulesByUserID(userID);
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        public ExecuteResult DeleteRole(int roleID)
        {
            roleDao.UpdateState(roleID, 0);
            return new ExecuteResult() { ResultNo = 1, Message = "" };
        }
        /// <summary>
        /// 删除角色管理权限
        /// </summary>
        public ExecuteResult DeleteModule2Action(int roleID)
        {
            module2ActionDao.DeleteByRoleID(roleID);
            return new ExecuteResult() { ResultNo = 1, Message = "" };
        }
        /// <summary>
        /// 获取角色
        /// </summary>
        public PowerRole GetRole(int roleID)
        {
            return roleDao.Find(roleID);
        }

        /// <summary>
        /// 获取菜单
        /// </summary>
        /// <returns></returns>
        public IList<PowerModule> GetMenu()
        {
            return moduleDao.FindMenu();
        }

        /// <summary>
        /// 获取所有角色列表
        /// </summary>
        public IList<PowerRole> GetRoles()
        {
            return roleDao.FindAll(this.OwnerID);
        }

        /// <summary>
        /// 获取所有模块
        /// </summary>
        public IList<PowerModule> GetModules()
        {
            return moduleDao.FindAll();
        }
        public static IList<PowerAction> GetActionListByModuleID(int moduleID)
        {
            return new PowerActionDao().FindByModuleID(moduleID);
        }
        /// <summary>
        /// 获取所有动作
        /// </summary>
        public IList<PowerAction> GetActions()
        {
            return actionDao.FindAll();
        }
        /// <summary>
        /// 獲取權限名
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public static string GetRoleNames(int userID)
        {
            int OwnerID = 0;
            string roleNames = "";
            Tasks.Business.PowerBLL powerlBll = new Business.PowerBLL(OwnerID);
            IList<PowerUser2Role> powerRolelist = powerlBll.GetRoleByUserID(userID);
            for (int i = 0; i < powerRolelist.Count; i++)
            {
                if (powerRolelist.Count > 0)
                {
                    int RoleID = powerRolelist[i].RoleID;
                    PowerRole powerName = powerlBll.GetRole(RoleID);
                    roleNames += powerName.Name + " ";
                }
            }
            return roleNames;
        }

        public ExecuteResult SaveMenuPower(IList<PWRole2Menu> menu, int roleId)
        {
            new PWRole2MenuDao().Insert(menu, roleId);
            return new ExecuteResult { ResultNo = 1, Message = "保存成功!" };
        }
        public IList<PWRole2Menu> GetMenuPowerByRoleID(int roleID)
        {
            return new PWRole2MenuDao().FindByRoleID(roleID);
        }

        public ExecuteResult DeleteRole2Menu(int roleID)
        {
            new PWRole2MenuDao().DeleteByRoleID(roleID);
            return new ExecuteResult { ResultNo = 1, Message = "" };
        }

        /// <summary>
        /// 获取当前操作者的子Menu权限
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public IList<PWRole2Menu> GetMenuPowerByUserID(int userID)
        {
            return new PWRole2MenuDao().FindByUserID(userID);
        }

        /// <summary>
        ///  获取当前操作者的父Menu权限
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public IList<PWRole2Menu> GetParentMenuPowerByUserID(int userID)
        {
            return new PWRole2MenuDao().FindParentIDByUserID(userID);
        }

        /// <summary>
        /// 获取父菜单
        /// </summary>
        /// <returns></returns>
        public IList<Menu> GetParentMenu()
        {
            return new MenuDao().FindByParentID(0);
        }

        /// <summary>
        /// 获取子菜单
        /// </summary>
        /// <returns></returns>
        public IList<Menu> GetChildMenu()
        {
            return new MenuDao().FindChild();
        }

        /// <summary>
        /// 通过用户权限获取子菜单
        /// </summary>
        /// <returns></returns>
        public IList<Menu> GetChildMenuByuserID(int userID)
        {
            return new MenuDao().FindChildByUserID(userID);
        }

        /// <summary>
        ///  通过用户权限获取父菜单
        /// </summary>
        /// <returns></returns>
        public IList<Menu> GetParentMenuByuserID(int userID)
        {
            return new MenuDao().FindParentByUserID(userID);
        }
    }
}
